import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useTokenStore = defineStore('token', () => {
  const token = ref(localStorage.getItem('admin_token') || '')
  
  const hasToken = computed(() => {
    return !!token.value && token.value.length > 0
  })
  
  function setToken(newToken) {
    token.value = newToken
    localStorage.setItem('admin_token', newToken)
    console.log('管理员Token已设置')
  }
  
  function clearToken() {
    token.value = ''
    localStorage.removeItem('admin_token')
    localStorage.removeItem('admin_user')
    console.log('管理员Token已清除')
  }
  
  return {
    token,
    hasToken,
    setToken,
    clearToken
  }
})